package logic

import (
	"context"
	"fmt"
	"week/common"
	"week/user-srv/user"

	"week/api/internal/svc"
	"week/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type RealAuthLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewRealAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RealAuthLogic {
	return &RealAuthLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RealAuthLogic) RealAuth(req *types.RealAuthRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	auth, err := l.svcCtx.UserSrv.RealAuth(l.ctx, &user.RealAuthRequest{
		UserId:   req.UserId,
		IdCard:   req.IdCard,
		RealName: req.RealName,
	})
	fmt.Println(req.UserId)
	if err != nil {
		return &types.Response{
			Code:    "-1",
			Message: err.Error(),
		}, nil
	}
	if auth.Success {
		return &types.Response{
			Code:    "-2",
			Message: "用户以实名或实名信息被使用",
		}, nil
	}
	if !common.RealAuth(req.IdCard, req.RealName) {
		return &types.Response{
			Code:    "-3",
			Message: "实名信息错误",
		}, nil
	}
	realAuth, err := l.svcCtx.UserSrv.SaveRealAuth(l.ctx, &user.SaveRealAuthRequest{
		UserId:   req.UserId,
		IdCard:   req.IdCard,
		RealName: req.RealName,
	})
	fmt.Println(realAuth)
	if err != nil {
		return &types.Response{
			Code:    "-4",
			Message: err.Error(),
		}, nil
	}
	return &types.Response{
		Code:    "0",
		Message: "实名认证成功",
		Data: map[string]interface{}{
			"realAuth": realAuth,
		},
	}, nil
}
